<?php

/*
 * 所有與討論區的資料操作都放在這裡
 */
class Application_Model_topic
{
	private $DBTable_topic;
	private $DBTable_discuss_topic;
	private $md_auth;
	
	public function __construct()
	{
		$this->DBTable_topic = new Zend_Db_Table('topic');
		$this->DBTable_discuss_topic = new Zend_Db_Table('discuss_topic');
		$this->md_auth = new Application_Model_auth();
	}
	
	//新增討論區文章。
	public function addTopic($ary_post)
	{
		$nowDT = Zend_Date::now()->toString('YYYY-MM-dd HH:mm:ss');
		
		switch($ary_post['sel_topicUsage'])
		{
			case '1':
				$ary_post['txt_title'] = '[問題] ' . $ary_post['txt_title'];
				break;
				
			case '2':
				$ary_post['txt_title'] = '[分享] ' . $ary_post['txt_title'];
				break;
				
			case '3':
				$ary_post['txt_title'] = '[心得] ' . $ary_post['txt_title'];
				break;
				
			case '4':
				$ary_post['txt_title'] = '[抱怨] ' . $ary_post['txt_title'];
				break;
			
			case '5':
				$ary_post['txt_title'] = '[其它] ' . $ary_post['txt_title'];
				break;
				
			case '6':
				$ary_post['txt_title'] = '[回報] ' . $ary_post['txt_title'];
				break;
		}
		
		$ary_addData = array(
			'topic_type' => 2,
			'topic_category_id' => $ary_post['hid_cId'],
			'topic_parent_id' => 0,
			'topic_subject' => $ary_post['txt_title'],
			'topic_creator_id' => $this->md_auth->getMemberId('u_id'),
			'topic_top' => 0,
			'topic_lock' => 0,
			'topic_reply_count' => 0,
			'topic_hit_count' => 0,
			'topic_last_reply' => '0000-00-00 00:00:00',
			'topic_last_replier_id' => 0,
			'topic_status' => 1,
			'topic_created' => $nowDT,
		);
		
		$newTopicId = $this->DBTable_topic->insert($ary_addData);
		
		$ary_addData = array(
			'topic_id' => $newTopicId,
			'dt_content' => $ary_post['txa_body']
		);
		
		$this->DBTable_discuss_topic->insert($ary_addData);
		
		$this->md_auth->doPostAmtIncrease();
	}
	
	/**
	 * 取得特定文章ID的討論串。
	 * @param $topic_id
	 * 
	 * @return Zend_Db_Select $DBSel
	 */
	public function getById($topic_id)
	{
		$DBSel = $this->DBTable_topic->getAdapter()
			->select()
			->from(array('t' => 'topic'))
			->joinLeft(array('d_t' => 'discuss_topic'), 't.topic_id = d_t.topic_id')
			->joinLeft(array('a_i' => 'addtional_info'), 't.topic_id = a_i.it_id')
			->joinLeft(array('u' => 'user'), 't.topic_creator_id = u.u_id')
			->where('t.topic_id =' . $topic_id)
			->orWhere('t.topic_parent_id =' . $topic_id)
			->order('t.topic_created ASC');
			
		return $DBSel;
	}
	
	/**
	 * 取得特定分區的1樓文章資料。
	 * @param $categoryId
	 * 
	 * @return Zend_Db_Select $DBSel
	 */
	public function getInEachCategory($categoryId)
	{
		$DBSel = $this->DBTable_topic->getAdapter()
			->select()
			->from(array('t' => 'topic'))
			->join(array('u' => 'user'), 't.topic_creator_id = u.u_id')
			->where('t.topic_category_id =' . $categoryId)
			->Where('t.topic_parent_id = 0')
			->order('t.topic_created DESC');
			
		return $DBSel;
	}
	
	/**
	 * 取得特定分區的1樓文章資料中，最後回覆者的資料。
	 * @param $categoryId
	 * 
	 * @return Zend_Db_Select $DBSel
	 */
	public function getInEachCategoryRespondent($categoryId)
	{
		$DBSel = $this->DBTable_topic->getAdapter()
			->select()
			->from(array('t' => 'topic'))
			->joinLeft(array('u' => 'user'), 't.topic_last_replier_id = u.u_id')
			->where('t.topic_category_id =' . $categoryId)
			->Where('t.topic_parent_id = 0')
			->order('t.topic_created DESC');
			
		return $DBSel;
	}
		
	/**
	 * 取得特定文章。
	 * @param str $topicId
	 * 
	 * @return Zend_Db_Table_Row $DBRow
	 */
	public function getTopic($topicId)
	{
		return $this->DBTable_topic->fetchRow('topic_id =' . (int)$topicId);
	}
	
	/**
	 * 取得特定引言。
	 * @param str $topicId
	 * 
	 * @return ary
	 */
	public function getQuotation($topicId)
	{
		return $this->getById($topicId)->query()->fetch();
	}
	
	/**
	 * 新增討論串的回覆。
	 */
	public function addReply($ary_post)
	{
		$nowDT = Zend_Date::now()->toString('YYYY-MM-dd HH:mm:ss');
		
		$ary_addData = array(
			'topic_type' => 2,
			'topic_category_id' => 1,
			'topic_parent_id' => (int)$ary_post['hid_topicId'],
			'topic_subject' => '',
			'topic_creator_id' => $this->md_auth->getMemberId('u_id'),
			'topic_top' => 0,
			'topic_lock' => 0,
			'topic_reply_count' => 0,
			'topic_hit_count' => 0,
			'topic_last_reply' => '0000-00-00 00:00:00',
			'topic_last_replier_id' => 0,
			'topic_status' => 1,
			'topic_created' => $nowDT,
		);
		
		$newTopicId = $this->DBTable_topic->insert($ary_addData);
		
		$ary_addData = array(
			'topic_id' => $newTopicId,
			'dt_content' => $ary_post['txa_body']
		);
		
		$this->DBTable_discuss_topic->insert($ary_addData);
		
		$this->doAfterReplyWork($ary_post);
	}
	
	/**
	 * 執行"主題回覆"或"引言回覆"後的紀錄功能。
	 */
	private function doAfterReplyWork($ary_post)
	{
		$topicId = (int)$ary_post['hid_topicId'];
		
		$DBRow = $this->DBTable_topic->fetchRow('topic_id =' . $topicId);
		$ary_updData = array(
			'topic_reply_count' => ($DBRow->topic_reply_count + 1),
			'topic_last_reply' => Zend_Date::now()->toString('YYYY-MM-dd HH:mm:ss'),
			'topic_last_replier_id' =>  $this->md_auth->getMemberId('u_id')
		);
		$this->DBTable_topic->update($ary_updData, 'topic_id =' . $topicId);
	}
}